# -*- coding:utf-8 -*-
"""
标注平台接口
"""
from __future__ import print_function, unicode_literals
import requests
from logger_utility.loggconfig import getLogger


logger = getLogger("CoverLabelApi")
URL = "http://192.168.64.18:9999/input_data/"


def send_data(data):
    """
    写入数据
    :param data:
    :return:
    """
    try:
        ret = requests.post(URL, json=data, timeout=3)
        ret_dict = ret.json()

    except Exception as e:
        logger.exception(e)


def send_kg_data(
        title,
        content,
        word_entity,  # [{"word": "4月","entity": []}]
        relation=[],  # {"relation": ["戴"], "ToValue": [39, "帽子自拍"], "value": [37, "毛宁"]}
        class_="娱乐",
        data_id="20180115",
        data_name="娱乐标注"
):
    """
    发送知识图谱数据
    :return:
    """
    data = {
        "dataItem": class_,
        "title": title,
        "dataset_id": data_id,
        "newsContent": content,
        "dataset_name": data_name,

        "totalWords": [{
            "paragraph": "0",
            "contents": [{
                "words": word_entity,
                "sentence": "0"
            }]
        }],

        "entityRelation": relation
    }
    send_data(data)


def send_kg_entity_config(class_config, version):
    """
    分类参数配置
    :param class_config: [{: [{: []}]}]
    :param version: 版本好
    :return:
    """
    URL = "http://192.168.64.18:9999/add_entity_options/"
    data = {
        "data": class_config,
        "version": version
    }
    try:
        ret = requests.post(URL, json=data, timeout=3)
        ret_dict = ret.json()
        print(ret_dict)

    except Exception as e:
        logger.exception(e)


def send_kg_relation_config(relation_config, version):
    """
    添加关系或者属性的配置
    :param relation_config:　[{'夫妻': ["夫", "妻"]}, {'结婚': ['时间', '地点', '男方', '女方']}]
    :param version:
    :return:
    """
    URL = "http://192.168.64.18:9999/add_relation_options/"
    data = {
        "data": relation_config,
        "version": version
    }
    try:
        ret = requests.post(URL, json=data, timeout=3)
        ret_dict = ret.json()
        print(ret_dict)

    except Exception as e:
        logger.exception(e)




if __name__ == '__main__':
    def send_data():
        words = ["舒淇", "据", "台湾", "媒体", "报道", "，", "舒淇", "2016年", "9月", "和", "冯德伦", "结婚", "，", "夫妻", "俩", "私下", "生活", "甜蜜蜜", "，", "婚后", "肚皮", "动静", "也", "备受", "外界", "关注", "。"]
        send_kg_data(title='舒淇结婚',
                    content=''.join(words),
                    word_entity= [{'word': i, 'entity': []} for i in words]
                  )

    def send_class():
        config = [
            {
                '人物':
                    [
                        {
                            '明星':
                                [
                                    '男明星',
                                    '女明星'
                                ]
                        },
                        {
                            '主持人':
                                [
                                    '男主持',
                                    '女主持',
                                ]
                        },
                        {
                            '歌手':
                                [
                                    '内地',
                                    '港台',
                                ]
                        }
                    ],
            },
            {
                "事件":
                    [
                        {
                            "婚姻":
                                [
                                    '离婚',
                                    '结婚',
                                    '出轨'
                                ]
                        },
                        {
                            "恋情":
                                [
                                    '分手',
                                    '热恋',
                                ]
                        }
                    ]
            },
            {
                "创作品":
                    [
                        '电影',
                        '小说',
                        '电视剧',
                        '话剧',
                        '综艺',
                    ]
            },
        ]


